﻿using Basic.FormCollect;
using Basic.FormModel;
using Basic.FormModel.AgentForm;
using Basic.FormModel.QueryColumn;
using Basic.FormModel.QueryTable;
using Basic.FormRemoteModel;
using Basic.FormService.Interface;
using WeDonekRpc.Helper;

namespace Basic.FormService.lmpl
{
    internal class PhyTableCreateService : IPhyTableCreateService
    {
        private readonly IQueryTableCollect _QueryTable;

        private readonly IQueryColumnCollect _QueryCol;

        private readonly IAgentFormCollect _AgentForm;

        public PhyTableCreateService ( IQueryTableCollect queryTable,
            IQueryColumnCollect queryCol,
            IAgentFormCollect agentForm,
            ICustomFormCollect form )
        {
            this._QueryTable = queryTable;
            this._QueryCol = queryCol;
            this._AgentForm = agentForm;
        }
        private PhyQueryTable[] _tables;
        private QueryColumnDto[] _cols;
        public void Check ( long formId )
        {
            this._tables = this._QueryTable.Gets(formId);
            if ( this._tables.IsNull() )
            {
                throw new ErrorException("form.query.table.null");
            }
            this._cols = this._QueryCol.GetsByFormId<QueryColumnDto>(formId);
            if ( this._cols.IsNull() )
            {
                throw new ErrorException("form.query.column.null");
            }
        }
        public void Create ()
        {
            this._tables.ForEach(c =>
            {
                AgentTableCol[] list = this._cols.Convert(a => a.TableId == c.Id && a.ShowMode == PhyColShowMode.Column, a => this._GetCol(a));
                this._AgentForm.CreateTable(c.TableName, c.TableName, list);
            });
        }
        private AgentTableCol _GetCol ( QueryColumnDto col )
        {
            AgentTableCol t = new AgentTableCol
            {
                Description = col.ColLabel,
                IsNull = col.IsNull,
                IsOnly = false,
                Name = col.ColName
            };
            if ( col.ColName == ConstDic.KeyColName )
            {
                t.IsOnly = true;
            }
            if ( col.DataType == PhyTableDataType.Number )
            {
                t.Type = typeof(decimal);
                t.Precision = col.Precision.GetValueOrDefault(0);
            }
            else if ( col.DataType == PhyTableDataType.Int )
            {
                t.Type = typeof(int);
            }
            else if ( col.DataType == PhyTableDataType.Bool )
            {
                t.Type = typeof(bool);
            }
            else if ( col.DataType == PhyTableDataType.DateTime )
            {
                t.Type = typeof(DateTime);
                t.DbType = "smalldatetime";
            }
            else if ( col.DataType == PhyTableDataType.Date )
            {
                t.Type = typeof(DateTime);
                t.DbType = "date";
            }
            else if ( col.DataType == PhyTableDataType.Long )
            {
                t.Type = typeof(long);
            }
            else if ( col.DataType == PhyTableDataType.Short )
            {
                t.Type = typeof(short);
            }
            else if ( col.DataType == PhyTableDataType.Varchar )
            {
                t.Type = typeof(string);
                if ( col.ColLen > 8000 )
                {
                    t.DbType = "text";
                }
                else
                {
                    t.DbType = "varchar";
                    t.Len = col.ColLen;
                }
            }
            else if ( col.DataType == PhyTableDataType.NVarchar )
            {
                t.Type = typeof(string);
                if ( col.ColLen > 8000 )
                {
                    t.DbType = "ntext";
                }
                else
                {
                    t.DbType = "nvarchar";
                    t.Len = col.ColLen;
                }
            }
            else if ( col.DataType == PhyTableDataType.Array )
            {
                t.Type = typeof(string[]);
                t.Len = col.ColLen == 0 ? 255 : col.ColLen;
            }
            return t;
        }
    }
}
